package com.industrial.loweff.service.impl;

import com.industrial.common.core.utils.StringUtils;
import com.industrial.loweff.domain.CityLevelCdCount;
import com.industrial.loweff.domain.JcQueryParam;
import com.industrial.loweff.mapper.LowEfficiencyEnterprisesAnalysisMapper;
import com.industrial.loweff.service.ILowEfficiencyEnterprisesAnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LowEfficiencyEnterprisesAnalysisServiceImpl implements ILowEfficiencyEnterprisesAnalysisService {

    @Autowired
    private LowEfficiencyEnterprisesAnalysisMapper lowEfficiencyEnterprisesAnalysisMapper;

    /**
     * <p>
     *  查询地图数据
     * </p>
     *
     * @param param
     * @return
     */
    @Override
    public List<CityLevelCdCount> queryMapData(JcQueryParam param) {
        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryMapData(param);
        return cityLevelCdCounts;
    }

    /**
     * 查询近五年的数据
     *
     * @param params
     * @return
     */
    @Override
    public Map<String,Object> queryFiveData(JcQueryParam params) {
        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryFiveData(params);
        Map<String, CityLevelCdCount> collect = new HashMap<>();
        List<String> xAxis = new ArrayList<>();
        List<String> cYAxis = new ArrayList<>();
        List<String> dYAxis = new ArrayList<>();
        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty()){
            collect = cityLevelCdCounts.stream().collect(Collectors.toMap(CityLevelCdCount::getYears, v -> v));
            for(Map.Entry<String, CityLevelCdCount> temp : collect.entrySet()){
                String key = temp.getKey();
                xAxis.add(key);

                CityLevelCdCount cityLevelCdCount = temp.getValue();
                String cCount = cityLevelCdCount.getcCount();
                cYAxis.add(cCount);
                String dCount = cityLevelCdCount.getdCount();
                dYAxis.add(dCount);
            }
        }
        Map<String,Object> result = new HashMap<>();
        result.put("xAxis",xAxis);
        result.put("cYAxis",cYAxis);
        result.put("dYAxis",dYAxis);
        return result;
    }

    /**
     * 查询低效能企业行业分布
     *
     * @param params
     * @return
     */
    @Override
    public Map<String,Object> queryLoweIndustry(JcQueryParam params) {
        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryLoweIndustry(params);
        Map<String, CityLevelCdCount> collect = new HashMap<>();
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty()){
            collect = cityLevelCdCounts.stream().collect(Collectors.toMap(CityLevelCdCount::getIndustryType, v -> v));
            for(Map.Entry<String, CityLevelCdCount> pp : collect.entrySet()){
                String hyName = pp.getKey();
                xAxis.add(hyName);
                CityLevelCdCount value = pp.getValue();
                yAxis.add(value.getdCount());
            }
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("x",xAxis);
        resultMap.put("y",yAxis);
        return resultMap;
    }

    /**
     * 低效能企业地市分布
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryLoweCity(JcQueryParam params) {
        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryLoweCity(params);
        if(cityLevelCdCounts != null && cityLevelCdCounts.isEmpty() && StringUtils.isNotBlank(params.getCompanyType())){
            cityLevelCdCounts = cityLevelCdCounts.stream().filter(k -> StringUtils.equals(k.getCompanyType(),params.getCompanyType())).collect(Collectors.toList());
        }
        Map<String, CityLevelCdCount> collect = new HashMap<>();
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty()){
            collect = cityLevelCdCounts.stream().collect(Collectors.toMap(CityLevelCdCount::getCity, v -> v));
            for(Map.Entry<String, CityLevelCdCount> pp : collect.entrySet()){
                String key = pp.getKey();
                xAxis.add(key);

                CityLevelCdCount value = pp.getValue();
                String s = value.getdCount();
                yAxis.add(s);
            }
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("x",xAxis);
        resultMap.put("y",yAxis);
        return resultMap;
    }

    /**
     * 低效能企业研发强度
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryLowCompanyYfQd(JcQueryParam params) {
        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryLowCompanyYfQd(params);

        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty() && StringUtils.isNotBlank(params.getCompanyType())){
            cityLevelCdCounts = cityLevelCdCounts.stream().filter(k -> StringUtils.equals(k.getCompanyType(),params.getCompanyType())).collect(Collectors.toList());
        }

        Map<String, CityLevelCdCount> collect = new HashMap<>();

        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();

        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty()){
            collect = cityLevelCdCounts.stream().collect(Collectors.toMap(CityLevelCdCount::getCity, v -> v));
            for(Map.Entry<String, CityLevelCdCount> temp : collect.entrySet()){
                String key = temp.getKey();
                xAxis.add(key);

                CityLevelCdCount value = temp.getValue();
                String yfjfqd = value.getYfjfqd();
                yAxis.add(yfjfqd);
            }
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("x",xAxis);
        resultMap.put("y",yAxis);
        return resultMap;
    }

    /**
     * <p>
     * 低效能占用土地面积占比情况
     * </p>
     *
     * @param params
     * @return
     */
    @Override
    public  List<Map<String, Object>> queryLowYdmmzb(JcQueryParam params) {
        List<Map<String, Object>> resultMap = lowEfficiencyEnterprisesAnalysisMapper.queryLowYdmmzb(params);
        if(resultMap != null && !resultMap.isEmpty() && StringUtils.isNotBlank(params.getCompanyType())){
            resultMap = resultMap.stream().filter(k -> {
                return StringUtils.equals(String.valueOf(k.get("company_type")),params.getCompanyType());
            }).collect(Collectors.toList());
        }
        return resultMap;
    }

    /**
     * 查询指标
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryIndex(JcQueryParam params) {
        List<CityLevelCdCount> cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryIndex(params);
        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty() && StringUtils.isNotBlank(params.getCompanyType())){
            cityLevelCdCounts = cityLevelCdCounts.stream().filter(k -> StringUtils.equals(k.getCompanyType(),params.getCompanyType())).collect(Collectors.toList());
        }
        Map<String, CityLevelCdCount> collect = new HashMap<>(16);
        List<String> xAxis = new ArrayList<>();
        List<String> yAxis = new ArrayList<>();
//        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty()){
//            collect = cityLevelCdCounts.stream().collect(Collectors.toMap(CityLevelCdCount::getYears, v -> v));
//            for(Map.Entry<String, CityLevelCdCount> temp :collect.entrySet() ){
//                String key = temp.getKey();
//                xAxis.add(key);
//                CityLevelCdCount value = temp.getValue();
//                yAxis.add(value.getScore1());
//            }
//        }

        if(cityLevelCdCounts != null && !cityLevelCdCounts.isEmpty()){
            for(CityLevelCdCount temp : cityLevelCdCounts ){
                String key = temp.getYears();
                xAxis.add(key);
                String score1 = temp.getScore1();
                yAxis.add(score1);
            }
        }

        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("x",xAxis);
        resultMap.put("y",yAxis);
        return resultMap;
    }

    /**
     * 低效能指标情况分布
     *
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> queryLoweIndexFbInfo(JcQueryParam params) {
        //查询平均值的数据
        CityLevelCdCount cityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryAvgData(params);
        List<String> indexName = new ArrayList<>();
        List<String> avgList = new ArrayList<>();
        List<String> currentList = new ArrayList<>();

        //查询当前值
        CityLevelCdCount currentCityLevelCdCounts = lowEfficiencyEnterprisesAnalysisMapper.queryCurrentData(params);
        indexName.add("用地销售收入");
        indexName.add("能耗销售收入");
        indexName.add("污染物排放销售收入");
        indexName.add("研发经费投入强度");
        indexName.add("全员劳动生产率");
        indexName.add("单位用地税收");

        //加入平均值
        if(cityLevelCdCounts != null){
            avgList.add(StringUtils.isBlank(cityLevelCdCounts.getScore7()) ? "0" : cityLevelCdCounts.getScore7());
            avgList.add(StringUtils.isBlank(cityLevelCdCounts.getScore2()) ? "0" : cityLevelCdCounts.getScore2());
            avgList.add(StringUtils.isBlank(cityLevelCdCounts.getScore3()) ? "0" : cityLevelCdCounts.getScore3());
            avgList.add(StringUtils.isBlank(cityLevelCdCounts.getScore4()) ? "0" : cityLevelCdCounts.getScore4());
            avgList.add(StringUtils.isBlank(cityLevelCdCounts.getScore5()) ? "0" : cityLevelCdCounts.getScore5());
            avgList.add(StringUtils.isBlank(cityLevelCdCounts.getScore1()) ? "0" : cityLevelCdCounts.getScore1());
        }else{
            avgList.add("0");
            avgList.add("0");
            avgList.add("0");
            avgList.add("0");
            avgList.add("0");
            avgList.add("0");
        }


        //当前值
        if(currentCityLevelCdCounts != null){
            currentList.add(StringUtils.isBlank(currentCityLevelCdCounts.getScore7()) ? "0" : currentCityLevelCdCounts.getScore7());
            currentList.add(StringUtils.isBlank(currentCityLevelCdCounts.getScore2()) ? "0" : currentCityLevelCdCounts.getScore2());
            currentList.add(StringUtils.isBlank(currentCityLevelCdCounts.getScore3()) ? "0" : currentCityLevelCdCounts.getScore3());
            currentList.add(StringUtils.isBlank(currentCityLevelCdCounts.getScore4()) ? "0" : currentCityLevelCdCounts.getScore4());
            currentList.add(StringUtils.isBlank(currentCityLevelCdCounts.getScore5()) ? "0" : currentCityLevelCdCounts.getScore5());
            currentList.add(StringUtils.isBlank(currentCityLevelCdCounts.getScore1()) ? "0" : currentCityLevelCdCounts.getScore1());
        }else{
            currentList.add("0");
            currentList.add("0");
            currentList.add("0");
            currentList.add("0");
            currentList.add("0");
            currentList.add("0");
        }
        Map<String,Object> resultMap = new HashMap<>(16);
        resultMap.put("indexName",indexName);
        resultMap.put("avgList",avgList);
        resultMap.put("currentList",currentList);
        return resultMap;
    }
}
